package com.lanren.familybookapp.ui.me.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanren.common.interfaces.AdapterItemClickView;
import com.lanren.familybookapp.R;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ManagerAdapter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/12 10:53
 * @Version 1.0
 */
public class InviteAdapter extends RecyclerView.Adapter<InviteAdapter.ContentHolder> {


    private Context mContext;
    private List<Map<String, String>> list;
    private LayoutInflater mLayoutInflater;
    private AdapterItemClickView adapterItemClickView;

    private boolean isDele;

    public InviteAdapter(Context mContext, List<Map<String, String>> list) {
        this.mContext = mContext;
        this.list = list;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setDele(boolean dele) {
        isDele = dele;
        notifyDataSetChanged();
    }

    public void setAdapterItemClickView(AdapterItemClickView adapterItemClickView) {
        this.adapterItemClickView = adapterItemClickView;
    }

    @NonNull
    @Override
    public ContentHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ContentHolder(mLayoutInflater.inflate(R.layout.item_book_member, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ContentHolder contentHolder, final int i) {

        contentHolder.bookMemberName.setText(list.get(i).get("userInvite"));

        if (isDele) {
            contentHolder.bookMemberDele.setVisibility(View.VISIBLE);
        } else {
            contentHolder.bookMemberDele.setVisibility(View.GONE);
        }

        contentHolder.bookMemberDele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterItemClickView != null) {
                    adapterItemClickView.onItemClick(i);

                }
            }
        });
    }

    public void remove(int position) {
        list.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public void add(int position, Map<String, String> data) {
        list.add(position, data);
        notifyItemInserted(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        TextView bookMemberName;
        ImageView bookMemberDele;

        public ContentHolder(@NonNull View itemView) {
            super(itemView);
            bookMemberName = itemView.findViewById(R.id.book_member_name);
            bookMemberDele = itemView.findViewById(R.id.book_member_dele);
        }
    }
}
